/**
 * Copyright 2013 ABSir's Studio
 * 
 * All right reserved
 *
 * Create on 2013-6-13 下午6:40:09
 */
package com.absir.android.app;

import java.io.IOException;
import java.util.Enumeration;

import android.content.Context;

import com.absir.core.kernel.KernelLang.BreakException;
import com.absir.core.kernel.KernelLang.CallbackBreak;
import com.absir.core.kernel.KernelLang.CallbackTemplate;
import com.absir.core.kernel.KernelLang.FilterTemplate;

import dalvik.system.DexFile;

/**
 * @author absir
 * 
 */
public class DalvikHelper {

	/**
	 * @param context
	 * @param callback
	 * @param filter
	 */
	public static void doScannerFilter(Context context, CallbackBreak<String> callback, FilterTemplate<String> filter) {
		try {
			DexFile dexFile = new DexFile(context.getPackageCodePath());
			Enumeration<String> it = dexFile.entries();
			while (it.hasMoreElements()) {
				String classname = it.nextElement();
				if (filter == null || filter.doWith(classname)) {
					callback.doWith(classname);
				}
			}

		} catch (BreakException e) {

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @param context
	 * @param basepath
	 * @param callback
	 * @param filter
	 */
	public static void doScannerFilter(Context context, String basepath, CallbackTemplate<String> callback, FilterTemplate<String> filter) {
		try {
			DexFile dexFile = new DexFile(context.getPackageCodePath());
			Enumeration<String> it = dexFile.entries();
			while (it.hasMoreElements()) {
				String classname = it.nextElement();
				if (classname.startsWith(basepath) && (filter == null || filter.doWith(classname))) {
					callback.doWith(classname);
				}
			}

		} catch (BreakException e) {

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
